Skip to content

Conversation

@dsfaccini
Copy link
Contributor

Replaces #3154

@dsfaccini dsfaccini changed the title implement implement FilePart.from_path Nov 19, 2025
if media_type is None:
media_type = 'application/octet-stream'

binary_content = BinaryContent(data=path.read_bytes(), media_type=media_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not on BinaryContent? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean the default?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dsfaccini I meant that read_path was intended to be a method on BinaryContent, not FilePart, so I'm curious why you went the other way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn, that wasn't an active decision, honestly I just mixed up which class the method was supposed to go on, and I think my brain just though it made more sense on the FilePart (i.e. FilePart.from_path instead of BinaryContent.from_path)

I'll fix it now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fixed now

@DouweM DouweM changed the title implement FilePart.from_path Add BinaryContent.from_path convenience method Nov 20, 2025
if media_type is None:
media_type = 'application/octet-stream'

return cls(data=path.read_bytes(), media_type=media_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use narrow_type here to get an BinaryImage in case it's an image

path = Path(path)
if not path.exists():
raise FileNotFoundError(f'File not found: {path}')
media_type, _ = guess_type(path.as_posix())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need the as_posix for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought I had a linter error from the pathlib.Path type, probably was something else

)


def test_file_part_from_path(tmp_path: Path):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_file_part_from_path(tmp_path: Path):
def test_binary_content_from_path(tmp_path: Path):

@DouweM DouweM merged commit d578b42 into pydantic:main Nov 20, 2025
29 checks passed
@dsfaccini dsfaccini deleted the filepart-from-path branch November 20, 2025 18:45
sebastiand-cerebras pushed a commit to sebastiand-cerebras/pydantic-ai that referenced this pull request Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants